package com.liuwei.assemble.controller;

import com.alibaba.fastjson.JSON;
import com.liuwei.assemble.common.Result;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 *
 * @author lykosliu
 * @date 2017/1/23
 */
@Slf4j
@RestController
@RequestMapping("dbs")
public class DbController{

    @GetMapping
    public Result getDb() throws IOException {
        String path = "db.json";
        //InputStream is = DbController.class.getResourceAsStream(path);
        Resource resource = new ClassPathResource(path);
        InputStream stream = resource.getInputStream();
        String content = IOUtils.toString(stream, Charset.defaultCharset());
        log.info("db.json:{}",content);
        return Result.success(JSON.parseArray(content,Db.class));
    }

    @Data
    public static class Db{
        private String name;
        private String remark;
        private String ip;
        private String port;
        private String db;
        private String user;
        private String password;
        private String packages;
    }


//    public static void main(String[] args) {
//        DataSourceConfig ds=new DataSourceConfig();
//        ds.setDbType(DbType.MYSQL);
//        ds.setDriverName(DbType.MYSQL.getDriverName());
//        ds.setUsername("root");
//        ds.setPassword("");
//        ds.setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8");
//        ConfigBuilder config = new ConfigBuilder(null, ds, null, null, null);
//        List<TableInfo> tableInfos =  config.getTableInfoList();
//        tableInfos.stream().forEach(System.out::print);
//    }
}
